﻿using Common;
using IBLL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DemoManageSys.Areas.Filters;
using Models.DTO;
using Microsoft.AspNetCore.Mvc;
using Models;

namespace DemoManageSys.Areas.API.Controllers
{
    [Area("API")]
    public class OrderDetailController : Controller
    {
        private IOrderDetailBLL _orderDetailBLL;

        public OrderDetailController(
            IOrderDetailBLL orderDetailBLL
            )
        {
            _orderDetailBLL = orderDetailBLL;
        }

        #region 获取的方法 (GetOrderDetailDTO)
        /// <summary>
        /// 获取的方法
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetOrderDetailDTO(int page, int limit)
        {
            int count;
            //调用BLL的查询方法
            List<GetOrderDetailDTO> getOrderDetailDTOs = _orderDetailBLL.GetOrderDetailDTO(page, limit, out count);

            ReturnResule result = new ReturnResule()
            {
                Code = 0,
                Msg = "获取成功",
                Data = getOrderDetailDTOs,
                Count = count
            };

            return Json(result);
        }
        #endregion 

        #region 获取的方法 (GetOrderDetailes)
        /// <summary>
        /// 获取的方法
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <param name="customerId"></param>
        /// <param name="orderId"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetOrderDetail(int page, int limit, string customerId, string orderId)
        {
            int count;
            //调用BLL的查询方法
            List<GetShopInfoDTO> getOrderDetailDTOs = _orderDetailBLL.GetOrderDetail(page, limit, customerId, orderId, out count);

            ReturnResule result = new ReturnResule()
            {
                Code = 0,
                Msg = "获取成功",
                Data = getOrderDetailDTOs,
                Count = count
            };

            return Json(result);
        }
        #endregion ()

        #region 添加的方法 (CreateOrderDetail)
        /// <summary>
        /// 添加的方法
        /// </summary>
        /// <param name="prod"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult CreateOrderDetail([FromForm] OrderDetail prod)
        {
            //实例化对象
            ReturnResule resule = new ReturnResule();
            string msg;//定义返回消息
            resule.IsSuccess = _orderDetailBLL.CreateOrderDetail(prod,out msg);//调用方法传值
            if (resule.IsSuccess)
            {
                resule.Code = 200;
                resule.Msg = msg;
                resule.Data = prod.Id;
            }
            else
            {
                resule.Msg = msg;

            }
            return Json(resule);
        }
        #endregion

        #region 删除的方法 (DeleteOrderDetail)
        /// <summary>
        /// 删除的方法
        /// </summary>
        /// <param name="id">订单详情ID</param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult DeleteOrderDetail(string id)
        {
            //实例化对象
            ReturnResule resule = new ReturnResule();
            if (string.IsNullOrWhiteSpace(id))
            {
                resule.Msg = "删除订单详情的id不能为空";
                return Json(resule);
            }

            resule.IsSuccess = _orderDetailBLL.DeleteOrderDetail(id);//调用方法传值
            if (resule.IsSuccess)
            {
                resule.Msg = "删除订单详情成功";
                resule.Code = 200;
            }
            else
            {
                resule.Msg = "删除订单详情失败";
            }
            return Json(resule);
        }
        #endregion

        #region 结算的方法 (UpdateOrderDetail)
        /// <summary>
        /// 结算的方法
        /// </summary>
        /// <param name="productName"></param>
        /// <param name="purchaseNumber"></param>
        /// <param name="customerInfoId"></param>
        /// <param name="customerMonty"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult UpdateOrderDetail(string productName, int purchaseNumber, string customerInfoId, int customerMonty)
        {
            //实例化对象bool UpdateOrderDetail()
            ReturnResule resule = new ReturnResule();
           
            string msg;//定义返回消息
            resule.IsSuccess = _orderDetailBLL.UpdateOrderDetail(productName, purchaseNumber, customerInfoId, customerMonty, out msg);//调用方法传值

            resule.Msg = msg;
            if (resule.IsSuccess)
            {
                resule.Code = 200;
            }

            return Json(resule);
        }
        #endregion
    }
}